package com.knowswift.issues.task;

import com.knowswift.common.bean.issue.po.Issue;
import com.knowswift.common.bean.message.po.Message;
import com.knowswift.common.bean.message.po.MessageSend;
import com.knowswift.common.bean.project.vo.ModulePermissionVO;
import com.knowswift.common.bean.project.vo.ProjectLimit;
import com.knowswift.common.common.GlobalProperty;
import com.knowswift.common.enums.IssueStatus;
import com.knowswift.common.utils.WrappedBeanCopier;
import com.knowswift.common.utils.getui.GeTuiUtils;
import com.knowswift.issues.service.MessageSendService;
import com.knowswift.issues.service.MessageService;
import com.knowswift.issues.service.ModulePermissionService;
import com.knowswift.security.account.User;
import com.knowswift.security.account.UserService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
@EnableAsync
public class AsyncTask {

    @Resource
    private UserService userService;

    @Resource
    private MessageService messageService;

    @Resource
    private MessageSendService messageSendService;

    @Resource
    private ModulePermissionService modulePermissionService;

    @Async("taskExecutor")
    public void asyncUpdate() {


    }


    @Async("taskExecutor")
    public void saveMassage(Issue issue) {

        List<User> list = userService.list();
        Set<User> users = new HashSet<>();
        for (User user : list) {
            boolean auditIssue = UserService.checkAuthority(user, "auditIssue");
            boolean projectIssues = UserService.checkAuthority(user, "projectIssues");
            if (auditIssue) {
                if (!projectIssues) {
                    users.add(user);
                } else {
                    List<ModulePermissionVO> permissionVOS = modulePermissionService.getBaseMapper().getByUserId(user.getUserId());
                    for (ModulePermissionVO permissionVO : permissionVOS) {
                        List<ProjectLimit> projectLimits = WrappedBeanCopier.JSONArrayToList(permissionVO.getProjects(), ProjectLimit.class);
                        for (ProjectLimit projectLimit : projectLimits) {
                            if (permissionVO.getModuleName().equals(issue.getModuleName())
                                    && projectLimit.getProjectName().equals(issue.getProjectName())) {
                                users.add(user);
                            }
                        }

                    }
                }
            }
        }
        Message message = new Message();
        String title = "防治上报记录等待审核";
        message.setMessageTitle(title);
        message.setRelationId(issue.getIssueId());
        message.setRelationType(1);
        String content = "您好，您有最新的防治项目【" + issue.getModuleName() + ">" + issue.getProjectName() + "】上报记录等待审核，请登录PC电脑端审核。";
        message.setMessageContent(content);
        boolean save = messageService.save(message);
        if (!save) {
            return;
        }
        MessageSend messageSend;
        for (User user : users) {
            messageSend = new MessageSend();
            messageSend.setMessageId(message.getMessageId());
            messageSend.setUserId(user.getUserId());
            messageSendService.save(messageSend);
            GeTuiUtils.pushSingle(title, content, user.getPushCid(), true);
        }
    }

    @Async("taskExecutor")
    public void saveAuditMassage(Issue issue) {
        Message message = new Message();
        message.setRelationType(GlobalProperty.MESSAGE_RELATION_TYPE_AUDIT);
        message.setRelationId(issue.getIssueId());
        User user = userService.getById(issue.getCreateUserId());
        MessageSend messageSend = new MessageSend();
        messageSend.setUserId(issue.getCreateUserId());
        String content;
        String title = "防治上报记录审核通过";
        if (issue.getIssueStatus().equals(IssueStatus.PASS.getValue())) {
            message.setMessageTitle(title);
            content = "您好，您上报的防治项目【" + issue.getModuleName() + ">" + issue.getProjectName() + "】记录已通过审核。";
        } else {
            title = "防治上报记录审核未通过";
            message.setMessageTitle(title);
            content = "您好，您上报的防治项目【" + issue.getModuleName() + ">" + issue.getProjectName() + "】记录未通过审核。";
        }
        message.setMessageContent(content);
        GeTuiUtils.pushSingle(title, content, user.getPushCid(), true);
        boolean save = messageService.save(message);
        if (!save) {
            return;
        }
        messageSend.setMessageId(message.getMessageId());
        messageSendService.save(messageSend);
    }
}
